import 'dart:math';

import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flame/particles.dart';
import 'package:flutter/material.dart';

/// 粒子系统
class Game7 extends FlameGame with PanDetector {
  final List<Color> colors = [
    Colors.red,
    Colors.yellowAccent,
    Colors.green,
  ];

  final List<Vector2> positions = [
    Vector2(-10, 10),
    Vector2(10, 10),
    Vector2(0, -14),
  ];

  late List<Sprite> sprites = [];

  // @override
  // Future<void> onLoad() async {
  //   // _createParticle();
  //   // _createMultiParticle();
  //   sprites = [];
  //   for (int i = 1; i <= 10; i++) {
  //     sprites.add(await Sprite.load('touch/star_${'$i'.padLeft(2, '0')}.png'));
  //   }
  //
  //   _createAcceleratedParticle(Vector2(size.x / 2, size.y / 2));
  // }

  @override
  void onPanDown(DragDownInfo info) {
    // _createIndicator(info.eventPosition.global);
    _createAcceleratedParticle(info.eventPosition.global);
  }

  double ds = 0;

  @override
  void onPanUpdate(DragUpdateInfo info) {
    // add(TouchIndicator(position: info.eventPosition.global));
    ds += info.delta.global.length;
    if (ds > 10) {
      _createAcceleratedParticle(info.eventPosition.global);
      ds = 0;
    }
  }

  _createParticle() {
    Paint circlePaint = Paint()..color = Colors.purple;
    Particle particle = MovingParticle(
      curve: Curves.easeIn,
      from: Vector2.zero(),
      to: Vector2(200, 0),
      child: CircleParticle(paint: circlePaint, radius: 20),
      lifespan: 3,
    );

    final ParticleSystemComponent psc = ParticleSystemComponent(
        particle: particle, position: Vector2(size.x / 2, size.y / 2));

    add(psc);
  }

  _createMultiParticle() {
    Particle particle = Particle.generate(
        count: 3,
        lifespan: 3,
        generator: (i) => PaintParticle(
            paint: Paint()..blendMode = BlendMode.difference,
            child: MovingParticle(
                child: CircleParticle(
                    radius: 20, paint: Paint()..color = colors[i]),
                from: positions[i],
                to: i == 0 ? positions.last : positions[i - 1],
                curve: Curves.easeIn)));
    final ParticleSystemComponent psc = ParticleSystemComponent(
        particle: particle, position: Vector2(size.x / 2, size.y / 2));

    add(psc);
  }

  _createIndicator(Vector2 position) async {
    SpriteAnimation indicator =
        SpriteAnimation.spriteList(sprites, stepTime: 1 / 15, loop: false);

    Particle particle = SpriteAnimationParticle(
      animation: indicator,
      size: Vector2(30, 30),
    );

    final ParticleSystemComponent psc = ParticleSystemComponent(
        particle: particle, position: position, anchor: Anchor.center);

    add(psc);
  }

  final Random _random = Random();
  Vector2 randomVector() {
    Vector2 base = Vector2.random(_random); //  (0, 0) ~ (1, 1)
    Vector2 fix = Vector2(-0.5, -0.5);
    base = base + fix; //  (-0.5, -0.5) ~ (0.5, 0.5)
    return base * 200;
  }

  Color randomColor() {
    return Color.fromARGB(
        255, _random.nextInt(255), _random.nextInt(255), _random.nextInt(255));
  }

  _createAcceleratedParticle(Vector2 position) {
    CircleParticle circleParticle =
        CircleParticle(paint: Paint()..color = randomColor(), radius: 2);

    Particle particle = Particle.generate(
        count: 20,
        lifespan: 1,
        generator: (i) => AcceleratedParticle(
            acceleration: randomVector(),
            speed: randomVector(),
            position: position,
            child: circleParticle));

    final ParticleSystemComponent psc =
        ParticleSystemComponent(particle: particle);

    add(psc);
  }
}
